﻿using EasyExcel;
using Utilities;
using Model.Data;

namespace WssClient.Item.ZhongFu
{
    public class ZhongFu : Item<ZhongFu, SalesStockOut>
    {

        public override ZhongFu Init()
        {
            this.title = "中福";
            this.type = ItemType.Type.ZHONG_FU;
            this.excelName = "zhongfu.xlsx";

            return this;
        }

        public override void Test()
        {
        }

        public override void Handler(RequestData requestData)
        {
            SalesStockOut salesStockOut = new SalesStockOut();

            string isGeneral = requestData.data.isGeneral;
            if("是".Equals(isGeneral))
            {
                salesStockOut.Analysis(requestData);

                Print(salesStockOut);
            }
        }

        public override void Print(SalesStockOut salesStockOut)
        {
            ExcelDoc Excel = new ExcelDoc(GVar.item.excelName);

            Excel.SetCell(2, 2, salesStockOut.date);
            Excel.SetCell(2, 7, salesStockOut.order);
            Excel.SetCell(3, 2, salesStockOut.customer);

            int idx = 5;
            int total = salesStockOut.products.Count;
            int nextIdx = total > 0 ? idx : idx + 1;

            for(int i = 0; i < total; i++)
            {
                Product product = salesStockOut.products[i];
                Excel.SetCell(nextIdx, 0, product.idx);
                Excel.SetCell(nextIdx, 1, product.name);
                Excel.SetCell(nextIdx, 4, product.packages);
                Excel.SetCell(nextIdx, 5, product.number);
                Excel.SetCell(nextIdx, 6, product.units);
                Excel.SetCell(nextIdx, 7, product.price);
                Excel.SetCell(nextIdx, 8, product.amount);

                nextIdx++;

                int detailsCount = product.monkeyHeavyNumberDetails.Count;
                if(detailsCount > 0)
                {
                    Excel.InsertBlankRow(nextIdx, 9, Excel.Npoi.InitCellStyle(isBottom: false));
                    Excel.Npoi.SetAlignLeft(nextIdx, 0);
                    Excel.SetCell(nextIdx, 0, "明细:");
                    nextIdx++;

                    for(int j = 0; j < detailsCount; j++)
                    {
                        Excel.InsertBlankRow(nextIdx, 9, Excel.Npoi.InitCellStyle(false, isBottom: false), true, "微软雅黑 Light", 11);
                        Excel.Npoi.SetAlignLeft(nextIdx, 0);
                        Excel.SetCell(nextIdx, 0, product.monkeyHeavyNumberDetails[j]);
                        nextIdx++;
                    }
                }

                if(i < total - 1)
                {
                    Excel.CopyRow(idx, nextIdx);
                }
            }

            Excel.SetCell(nextIdx, 2, salesStockOut.packagesTotal);
            Excel.SetCell(nextIdx++, 6, CommonHelper.FormatDouble(salesStockOut.numberTotal));

            Excel.SetCell(nextIdx, 2, $"￥{salesStockOut.amountToal}");  //合计金额
            Excel.SetCell(nextIdx++, 5, salesStockOut.upperCaseAmount); //大写

            Excel.SetCell(nextIdx, 2, salesStockOut.lastIssueArrears); //上期欠款
            Excel.SetCell(nextIdx++, 6, salesStockOut.addUpArrears); //累计欠款

            Excel.SetCell(nextIdx, 2, salesStockOut.billingPerson); //开单人

            if(GVar.printMode)
            {
                Excel.Print();
            }
            else
            {
                Excel.PrintPreView();
            }
        }
    }
}
